import os, sys
from LAC import LAC
import time


def show_time(start_t, p):
    now_t = time.time()
    t = now_t - start_t
    ana = (t / p) * (1 - p)
    print("已用时 %.2f 秒(%.1f 分钟)" % (t, t / 60), end='')
    print("预计剩余 %.2f 秒(%.1f 分钟)，预计完成时间 %s." % (ana, ana / 60, time.asctime(time.localtime(now_t + ana))))


print("正在初始化...")
lac = LAC()

def kind_list_to_a_kind(kind_list:list):
    if len(kind_list) == 1:
        return kind_list[0]
    else:
        pass



def main():
    if len(sys.argv) < 3:
        while True:
            in_file_path = input("输入词向量数据集路径：\n>").replace('"', '')
            if os.path.isfile(in_file_path) is True:
                break

        while True:
            out_path = input("输入输出文件夹路径：\n>").replace('"', '')
            if os.path.isdir(out_path) is True:
                break
    else:
        in_file_path = sys.argv[1].replace('"', '')
        out_path = sys.argv[2].replace('"', '')

        if os.path.isfile(in_file_path) and os.path.isdir(out_path):
            print("正确的路径.")
        else:
            print("用法：directory.py 输入数据集路径 输出文件夹")

    # ################### 索引词典制作 第 1 步#################################
    start_time = time.time()
    with open(in_file_path, 'rb')as f:
        f_o = open(os.path.join(out_path, "sim.index.txt"), 'w+', encoding='utf-8')
        line_cnt = 0
        cnt = 0

        before_cnt = 0
        # 遍历文件所有行
        while True:
            line = f.readline()

            if f.tell() == before_cnt:
                break
            else:
                before_cnt = f.tell()
            f_o.write("%s %d %d\n" % (line.split(b' ')[0].decode('utf-8'), cnt, cnt+len(line)-1))

            # 输出一下
            if cnt % 100000 == 0:
                print(line_cnt, "%d%% " % ((line_cnt / 8824330) * 100), line.split(b' ')[0].decode('utf-8'), '\t', end='')
                show_time(start_t=start_time, p=((line_cnt + 1) / (8824330 + 1)))

            # 循环累加
            cnt += len(line)-1  # 走完本行
            cnt += 1  # 下一行的起始位置
            line_cnt += 1

        f_o.close()

    # ################### 分类词典制作 第 2步#################################
    kinds = [
        "n",  # 普通名词
        "f",  # 方位名词
        "s",  # 处所名词
        "nw",  # 作品名

        "nz",  # 其他专名
        "v",  # 普通动词
        "vd",  # 动副词
        "vn",  # 名动词

        "a",  # 形容词
        "ad",  # 副形词
        "an",  # 名形词
        "d",  # 副词

        "m",  # 数量词
        "q",  # 量词
        "r",  # 代词
        "p",  # 介词

        "c",  # 连词
        "u",  # 助词
        "xc",  # 其他虚词
        "w",  # 标点符号

        "PER",  # 人名
        "LOC",  # 地名
        "ORG",  # 机构名
        "TIME",  # 时间
    ]



if __name__ == '__main__':
    main()
